class Party {
    constructor() {
        this.leader = null;
        this.roomName = "";
        this.member = [];
        this.data = new Map();
    }

    size() {
        return this.data.size;
    }

    setLeader(player) {
        const { uid } = player;
        this.leader = uid;
        this.roomName = `party-${player.name}`;
        this.data.set(uid, player);
    }

    join(player) {
        const { uid } = player;
        this.member.push(uid);
        this.data.set(uid, player);
    }

    leave(uid) {
        // const player = this.data.get(uid);
        // player.clearPartyInfo();
        this.member.remove(uid);
        this.data.delete(uid);
    }

    /**
     * 解散
     */
    dissolve() {
        this.data.clear();
    }

    getRoom() {
        return this.roomName;
    }

    leaderName() {
        return this.data.get(this.leader).name;
    }

    makeData() {
        const data = {};
        data.member = [];
        data.leader = this.data.get(this.leader).uid;
        data.member = this.member;
        data.leaderName = this.leaderName();
        return data;
    }

    getData(uid) {
        return this.data.get(uid);
    }

}

Array.prototype.remove = function (element) {
    for (; ;) {
        const index = this.indexOf(element);
        if (index >= 0) {
            this.splice(index, 1);
        } else {
            return this;
        }
    }
}

module.exports = Party;